package com.lanou3g.code0502;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

/**
 * Created by dllo on 18/5/2.
 */
public class Main {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection
                    ("jdbc:mysql://localhost:3306/lanou3",
                            "root", "123456");
            //开启事务
            //把autoCommit关闭,相当于使用了begin语句
            conn.setAutoCommit(false);
            String sql1 =
                    "UPDATE bank\n" +
                            "SET money = money - 5\n" +
                            "WHERE cname = '鸣人'";
            String sql2 = "UPDATE bank\n" +
                    "SET money = money + 5\n" +
                    "WHERE cname = '佐助'";
            Statement statement = conn.createStatement();
            statement.executeUpdate(sql1);//执行SQL1
            statement.executeUpdate(sql2);//执行SQl2
            conn.commit();//关闭事务
            statement.close();//关闭资源
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
